package com.dd.cloud.user.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.cloud.common.constants.AdminConstants;
import com.dd.cloud.common.enums.ZkExceptionEnum;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.utils.ObjectUtils;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.user.entity.admin.AdminUser;
import com.dd.cloud.user.entity.admin.ResourceSet;
import com.dd.cloud.user.mapper.admin.ResourceSetMapper;
import com.dd.cloud.user.req.admin.AddOrUpdateResourceSetReq;
import com.dd.cloud.user.req.admin.GetSourcePageReq;
import com.dd.cloud.user.service.admin.IResourceSetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 联系我们功能表 服务实现类
 * </p>
 *
 * @author czg
 * @since 2023-06-09
 */
@Service
public class ResourceSetServiceImpl extends ServiceImpl<ResourceSetMapper, ResourceSet> implements IResourceSetService {

    /**
     * @description 添加或修改基础设置
     * @param req:
     * @return void
     * @author czg
     * @date 2023/6/9 15:07
    */
    @Override
    public void resourceAddOrUpdate(AddOrUpdateResourceSetReq req) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.ADMIN).get();
        ResourceSet resourceSet = new ResourceSet();
        if (req.getId() != null){
            resourceSet = getById(req.getId());
            if (resourceSet == null){
                throw new ZekeException("基础设置id不存在");
            }
            if (!req.getTitle().equals(resourceSet.getTitle())){
                QueryWrapper<ResourceSet> qw = new QueryWrapper<>();
                qw.lambda().eq(ResourceSet::getTitle, req.getTitle())
                        .ne(ResourceSet::getStatus, AdminConstants.ResourceSetStatus.DEL);
                ResourceSet one = getOne(qw);
                if (one != null){
                    throw new ZekeException("标题已存在");
                }
            }
            BeanUtils.copyProperties(req, resourceSet);
            resourceSet.setLastOperId(loginInfo.getId());
            resourceSet.setUpdateDate(LocalDateTime.now());
        } else {
            QueryWrapper<ResourceSet> qw = new QueryWrapper<>();
            qw.lambda().eq(ResourceSet::getTitle, req.getTitle())
                    .ne(ResourceSet::getStatus, AdminConstants.ResourceSetStatus.DEL);
            ResourceSet one = getOne(qw);
            if (one != null) {
                throw new ZekeException("标题已存在");
            }
            BeanUtils.copyProperties(req, resourceSet);
            resourceSet.setCreateDate(LocalDateTime.now());
            resourceSet.setOperId(loginInfo.getId());
            resourceSet.setStatus(AdminConstants.ResourceSetStatus.NORMAL);

        }
        boolean b = saveOrUpdate(resourceSet);
        if (!b){
            throw new ZekeException("操作失敗");
        }
    }

    /**
     * 分页查询基础设置
     */
    @Override
    public Page<ResourceSet> resourcePage(GetSourcePageReq req) {
        Page<ResourceSet> page = req.pagePojo();
        if (StringUtils.isBlank(req.getStatus())){
            req.setStatusStr(req.getStatus().split(","));
        }
        page = getBaseMapper().resourcePage(page, req);
        return page;
    }

    /**
     * @description 修改基础设置状态
     * @param id:
    	 * @param status:
     * @return void
     * @author czg
     * @date 2023/6/9 16:08
    */
    @Override
    public void resourceUpdate(Integer id, Integer status) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.ADMIN).get();
        ResourceSet byId = getById(id);
        if (byId == null) {
            throw new ZekeException("基础设置不存在");
        }
        if (status.equals(byId.getStatus())) {
            return;
        }
        if (!ObjectUtils.refrect(AdminConstants.ResourceSetStatus.class, status)) {
            throw new ZekeException(ZkExceptionEnum.STATUS_DATA_ERROR);
        }
        byId.setStatus(status);
        boolean i = updateById(byId);
        if (!i) {
            throw new ZekeException(ZkExceptionEnum.ADD_ERROR);
        }
    }

    /**
     * @description 查询协议内容
     * @param type:
     * @return ResourceSet
     * @author czg
     * @date 2023/6/9 17:42
    */
    @Override
    public ResourceSet resourceGetByType(Integer type) {
        QueryWrapper<ResourceSet> qw = new QueryWrapper<>();
        qw.lambda().eq(ResourceSet::getType, type)
                .eq(ResourceSet::getStatus, AdminConstants.ResourceSetStatus.NORMAL)
                .last("limit 1")
                .orderByDesc(ResourceSet::getUpdateDate);
        ResourceSet one = getOne(qw);
        return one;
    }

}
